package com.xlh.controller;

import com.xlh.common.Result;
import com.xlh.dto.PageDTO;
import com.xlh.dto.SystemProblemReportDTO;
import com.xlh.param.ReportSystemProblemParam;
import com.xlh.security.UserInfoHolder;
import com.xlh.service.SystemProblemReportService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author zhr 2019/1/28 问题反馈相关url请求类
 */
@Api(tags = "SystemProblemReport", description = "问题报告")
@RestController
public class SystemProblemReportController {

    @Autowired
    private SystemProblemReportService systemProblemReportService;

    @Autowired
    private UserInfoHolder userInfoHolder;

    @PostMapping("/system-problem")
    @ApiOperation("报告系统问题")
    @PreAuthorize("@permissionValidator.isTeacherOrStudent()")
    public Result reportSystemProblem(@RequestBody @Valid ReportSystemProblemParam reportSystemProblemParam) {
        systemProblemReportService.reportSystemProblem(reportSystemProblemParam, userInfoHolder.getUserId());
        return Result.success();
    }

    @GetMapping("/system-problem")
    @ApiOperation("系统问题列表展示")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result<PageDTO<SystemProblemReportDTO>> listSystemProblem(
            @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        PageDTO<SystemProblemReportDTO> list = systemProblemReportService.listSystemProblem(pageNum, pageSize);
        return Result.success(list);
    }

    @GetMapping("/system-problem/excel")
    @ApiOperation("下载问题反馈内容")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result downloadSystemProblem() {
        return systemProblemReportService.downloadSystemProblem();
    }
}
